package org.lsda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsda.sched.LsdaScheduler;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;

import com.google.gson.JsonObject;

/**
 * Servlet implementation class CreateJob
 */
@WebServlet("/CreateJob")
public class CreateJob extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateJob() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String jobGroup = request.getParameter("JobGroup");
		String jobName = request.getParameter("JobName");
		String description = request.getParameter("JobDesc");
		JobDetail jd = null;
		List<? extends Trigger> triggers = null;
		JsonObject json = new JsonObject();
		json.addProperty("Result", "OK");
		
		try {
			LsdaScheduler.addJob(jobName, jobGroup, description);
			
			jd = LsdaScheduler.getJobDetail(jobGroup, jobName);
			triggers = LsdaScheduler.getTriggers(jobGroup, jobName);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", e.getMessage());
			e.printStackTrace();
			PrintWriter writer = response.getWriter();
//			System.out.println(json.toString());
			writer.write(json.toString());
			return;
		}
		
		
				
		JsonObject record = new JsonObject();
		record.addProperty("JobName",
				jd.getJobDataMap().getString("jobId"));
		record.addProperty("JobGroup",
				jd.getJobDataMap().getString("groupId"));
		record.addProperty("JobDesc", jd.getJobDataMap().getString("description"));
		record.addProperty("UpdateTime",
				jd.getJobDataMap().getString("updateTime"));
		if(triggers == null || triggers.size()==0)
			record.addProperty("JobStatus", 0);
		else
			record.addProperty("JobStatus", 1);
		
		json.add("Record", record);
		PrintWriter writer = response.getWriter();
//		System.out.println(json.toString());
		writer.write(json.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
//		Enumeration<String> es = request.getParameterNames();
//
//		while (es.hasMoreElements()) {
//			String name = es.nextElement();
//			System.out.println("createjob " + name + "="
//					+ request.getParameter(name));
//		}
	}

}
